import { Component,OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {HttpURLService} from "../../httpURL.service";

/**
 * 角色管理
 * guopeng
 * 2017.11.27
 */
export class Role{
  constructor(
    private roleId : string,//角色ID
    private roleName : string,//角色名
    private roleStatus : string,//角色状态
    private roleDesc :string,//角色描述
    private createTime : string,//创建时间
    private updateTime : string,//更新时间
    private extensionInfo : string//预留字段
  ){}
}

@Component({
  selector: 'app-role',
  templateUrl: './role.component.html',
  styleUrls: ['./role.component.css']
})
export class RoleComponent implements OnInit{
  private roleName : string;//角色名
  private roleStatus : string;//角色状态
  errorMessage:string;//錯誤消息
  private list:Array<any>;
  queryMessage:Role = new Role("","","","","","","");
  updateMessages:Role = new Role("","","","","","","");
  public addMessages:Role =  new Role("","","","","","","");
  public params; // 保存页面url参数
  public pageSize:any = 5;// 每页数据条数
  public curPage:any = 1;// 当前页码
  public totalNum: any = 0; // 总数据条数
  public totalPage: any = 0;// 总页数
  public url:string = this.httpURLService.getUrl("communication");
  constructor(private http: HttpClient,private httpURLService: HttpURLService) {

  }
  add(){
    this.addMessages =  new Role("","","","","","","");
  }

  userMessage(event:any){
    let query = new Role("","","","","","","");
    let update = new Role("","","","","","","");
    Object.assign(query, event);
    Object.assign(update, event);
    this.queryMessage = query;
    this.updateMessages = update;
  }

  listMessage(info:any){
    let roleName = info.roleName== undefined ? '' : info.roleName ;
    let roleStatus = info.roleStatus== undefined ? '' : info.roleStatus;
    this.http.get(this.url+'/role/queryRoleListByPage?roleName='+roleName+'&roleStatus='+roleStatus).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

  addMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/role/addRole',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#add").modal('show');
          this.listMessage("");
          $('#addModal').modal('hide');
        }else if(data['resultCode']=='101113001'){
          this.errorMessage = "角色名为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101113003'){
          this.errorMessage = "角色名已存在！";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }

  deleteById(event:any){
    let id = event.target.name;
    this.http.get(this.url+'/role/deleteRole/'+id).subscribe(data =>{
      if(data['resultCode']=='0'){
        $("#delete").modal('show');
        this.listMessage("");
        $('#deleteModal').modal('hide');
      }else if(data['resultCode']=='101113002'){
        this.errorMessage = "该角色不存在！";
        $("#error").modal('show');
      }
    })
  }

  updateMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/role/updateRole',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#update").modal('show');
          this.listMessage("");
          $('#updateModal').modal('hide');
        }else if(data['resultCode']=='101113001'){
          this.errorMessage = "角色名为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101113002'){
          this.errorMessage = "该角色不存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101113003'){
          this.errorMessage = "角色名已存在！";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }
  getPageData(pageNo) {
    let vm = this;
    vm.curPage = pageNo;
    let roleName = this.roleName== undefined ? '' : this.roleName ;
    let roleStatus = this.roleStatus== undefined ? '' : this.roleStatus;
    let current = pageNo;
    this.http.get(this.url+'/role/queryRoleListByPage?roleName='+roleName+'&roleStatus='+roleStatus+'&current='+current).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

	onstructor(){ }
	ngOnInit(){
    this.http.get(this.url+'/role/queryRoleListByPage').subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
	}
}


